package com.blink.optimize.apis.collect;

import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * <Description> </Description>
 * <ClassName> TestCompareLinkedListAndArrayList</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月08日 10:47
 */
public class TestCompareLinkedListAndArrayList {


    @Test
    public void testLinkedList() {
        long start = System.currentTimeMillis();
        LinkedList linkedList = new LinkedList();
        Object o = new Object();
        for (int i = 0; i < 5_000_000; i++) {
            linkedList.add(o);
        }
        System.out.println(System.currentTimeMillis() - start);
    }

    @Test
    public void testArrayList() {
        long start = System.currentTimeMillis();
//        ArrayList arrayList = new ArrayList();
        ArrayList arrayList = Lists.newArrayListWithExpectedSize(5_000_000);//避免扩容时间消耗
        Object o = new Object();
        for (int i = 0; i < 5_000_000; i++) {
            arrayList.add(o);
        }
        System.out.println(System.currentTimeMillis() - start);
    }

    @Test
    public void testLinkedListAddFirst() {
        long start = System.currentTimeMillis();
//        ArrayList arrayList = new ArrayList();
        LinkedList linkedList = new LinkedList();
        Object o = new Object();
        for (int i = 0; i < 10_000; i++) {
            linkedList.add(0, o);
        }
        System.out.println(System.currentTimeMillis() - start);
    }

    @Test
    public void testArrayListAddFirst() {
        long start = System.currentTimeMillis();
//        ArrayList arrayList = new ArrayList();
        ArrayList arrayList = Lists.newArrayListWithExpectedSize(1_000);//避免扩容时间消耗
        Object o = new Object();
        for (int i = 0; i < 10_000; i++) {
            arrayList.add(0, o);
        }
        System.out.println(System.currentTimeMillis() - start);
    }

    @Test
    public void testLinkedListRemove() {

        LinkedList linkedList = new LinkedList();
        Object o = new Object();
        for (int i = 0; i < 5_000_000; i++) {
            linkedList.add(o);
        }
        long start = System.currentTimeMillis();
        linkedList.remove(2_500_000);
        System.out.println(System.currentTimeMillis() - start);
    }

    @Test
    public void testArrayListRemove() {
//        ArrayList arrayList = new ArrayList();
        ArrayList arrayList = Lists.newArrayListWithExpectedSize(5_000_000);//避免扩容时间消耗
        Object o = new Object();
        for (int i = 0; i < 5_000_000; i++) {
            arrayList.add(o);
        }
        long start = System.currentTimeMillis();
        arrayList.remove(2_500_000);
        System.out.println(System.currentTimeMillis() - start);
    }
}
